#include "my_music.h"

My_music::My_music(QWidget *parent) : QWidget(parent)
{
    music = new QMediaPlayer(this);
    musiclist = new QMediaPlaylist(this);
    
    music->setPlaylist(musiclist);
    
    musiclist->setPlaybackMode(QMediaPlaylist::Sequential);
    
    // 连接信号
    connect(music, &QMediaPlayer::positionChanged, this, &My_music::positionChanged);
    connect(music, &QMediaPlayer::mediaStatusChanged, this, &My_music::mediaStatusChanged);
    connect(musiclist, &QMediaPlaylist::currentIndexChanged, this, &My_music::currentIndexChanged);
    

}

My_music::~My_music()
{
    delete music;
    delete musiclist;
}

void My_music::addMedia(const QUrl &url)
{
    musiclist->addMedia(url);
}

void My_music::clearPlaylist()
{
    musiclist->clear();
}

void My_music::setPlaybackMode(QMediaPlaylist::PlaybackMode mode)
{
    musiclist->setPlaybackMode(mode);
}

void My_music::setVolume(int volume)
{
    music->setVolume(volume);
}

void My_music::play()
{
    music->play();
}

void My_music::pause()
{
    music->pause();
}

void My_music::stop()
{
    music->stop();
}

void My_music::setCurrentIndex(int index)
{
    musiclist->setCurrentIndex(index);
}

int My_music::currentIndex() const
{
    return musiclist->currentIndex();
}

QMediaPlayer::State My_music::state() const
{
    return music->state();
}


